﻿using System;
using System.Windows.Forms;
using MapWindow.Map;
using MapWindow.Components;
using MapWindow.PluginInterfaces;
using MapWindow.Data;

namespace FindCountryPlugin
{
    /// <summary>
    /// This MapWindow 6 plug-in demonstrates how to add data to the map
    /// </summary>
    [MapWindowPlugin("Find Country", Author="JK", UniqueName="FindCountryPlugin_1", Version="1.0")]
    public class Class1 : Extension, IMapPlugin
    {
        #region Variables

        //the file name of the countries shapefile. Change the file name to the
        //actual path where the file is stored on your computer.
        private string _countryFileName = "c:\\dev\\SampleData\\world_countries.shp";

        //the map layer of the countries
        private IMapFeatureLayer _countriesLayer;
        
        //the class level variable for accessing the main application
        private IMapPluginArgs _mapWin;

        //the menu item added by the plug-in
        private ToolStripMenuItem menuItem1;

        #endregion

        #region IMapPlugin Members

        //when the plug-in is activated
        public void Initialize(IMapPluginArgs args)
        {
            _mapWin = args;
            
            //add a new menu item
            menuItem1 = new ToolStripMenuItem("Find Country");
            //add a sub-menu item
            ToolStripMenuItem menuItem2 = new ToolStripMenuItem("Add data to map");
            menuItem1.DropDownItems.Add(menuItem2);
            
            //handle the menu item click event
            menuItem2.Click += new EventHandler(menuItem2_Click);

            //add a 'find country' menu item
            ToolStripMenuItem menuItem3 = new ToolStripMenuItem("Find country");
            menuItem1.DropDownItems.Add(menuItem3);
            menuItem3.Click += new EventHandler(menuItem3_Click);

            _mapWin.MainMenu.Items.Add(menuItem1);
        }

   
        #endregion

        #region Extension Members

        //when the plug-in is deactivated
        protected override void OnDeactivate()
        {
            //remove the menu item
            if (_mapWin.MainMenu.Items.Contains(menuItem1))
            {
                _mapWin.MainMenu.Items.Remove(menuItem1);
            }
        }

        #endregion

        #region Event Handlers

        //when the menu item is clicked
        void menuItem2_Click(object sender, EventArgs e)
        {
            //open a feature set
            FeatureSet fs = new FeatureSet();
            fs.Open(_countryFileName);
            //add the feature set to the map as a new layer
            _countriesLayer = _mapWin.Map.Layers.Add(fs);
        }

        //when the 'find country' menu item is clicked
        void menuItem3_Click(object sender, EventArgs e)
        {
            QueryForm frm = new QueryForm(_countriesLayer, _mapWin);
            frm.Show();
        }

        //This function opens a shapefile and adds it to the map
        private void AddShapefileToMap(string fileName)
        {
            FeatureSet fs = new FeatureSet();
            fs.Open(fileName);
            _mapWin.Map.Layers.Add(fs);
            fs.FillAttributes();
        }

        #endregion
    }
}
